package com.mjfeed.fragment;

import com.mjfeed.activity.FavoriteListActivity;
import com.mjfeed.activity.MainActivity;
import com.mjfeed.activity.OrderListActivity;
import com.mjfeed.activity.PasswordActivity;
import com.mjfeed.activity.ProfileActivity;
import com.mjfeed.activity.R;
import com.mjfeed.activity.ReceiverListActivity;
import com.mjfeed.activity.RegisterLoginActivity;
import com.mjfeed.util.SPUtils;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

/**
 * 会员Fragment
 * @author zhaominglei
 * @date 2014-12-8
 * 
 */
public class MemberFragment extends BaseFragment implements OnClickListener{
	@SuppressWarnings("unused")
	private static final String TAG = MemberFragment.class.getSimpleName();
	private View view;
	private MainActivity activity;
	
	private Button registerLogin; //登陆/注册
	private TextView usernameText; //用户名Text
	private String username; //用户名
	private String name; //姓名
	private String sign; //签名
	private RelativeLayout order; //我的订单
	private RelativeLayout receiver; //收货地址
	private RelativeLayout favorite; //我的关注
	private RelativeLayout profile; //个人资料
	private RelativeLayout editpass; //修改密码
	private Button logout; //安全退出
	
	private Intent intent;
	
	@Override
	public View onCreateView(LayoutInflater inflater,
			@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.fragment_member, container, false);
		return view;
	}

	@Override
	public void onActivityCreated(@Nullable Bundle savedInstanceState) {
		view = getView();
		activity = (MainActivity)getActivity();
		super.onActivityCreated(savedInstanceState);
		
		init();
	}
	
	private void init() {
		registerLogin = (Button) view.findViewById(R.id.member_register_login);
		usernameText = (TextView) view.findViewById(R.id.member_username);
		order = (RelativeLayout) view.findViewById(R.id.member_order);
		receiver = (RelativeLayout) view.findViewById(R.id.member_receiver);
		favorite = (RelativeLayout) view.findViewById(R.id.member_favorite);
		profile = (RelativeLayout) view.findViewById(R.id.member_profile);
		editpass = (RelativeLayout) view.findViewById(R.id.member_editpass);
		logout = (Button) view.findViewById(R.id.member_logout);
		
		registerLogin.setOnClickListener(this);
		order.setOnClickListener(this);
		receiver.setOnClickListener(this);
		favorite.setOnClickListener(this);
		profile.setOnClickListener(this);
		editpass.setOnClickListener(this);
		logout.setOnClickListener(this);
		
		username = (String)SPUtils.get(activity, "username", "");
		name = (String)SPUtils.get(activity, "name", "");
		sign = (String)SPUtils.get(activity, "sign", "");
		if (username != null && !username.equals("") && sign != null && !sign.equals("")) {
			String showName = (name != null && !name.equals("")) ? name : username;
			usernameText.setText(showName+getResources().getString(R.string.member_welcome_msg));
			usernameText.setVisibility(View.VISIBLE);
			registerLogin.setVisibility(View.GONE);
			logout.setVisibility(View.VISIBLE);
		}
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.member_register_login:
			intent = new Intent(activity, RegisterLoginActivity.class);
			startActivity(intent);
			break;
			
		case R.id.member_order:
			if (username != null && !username.equals("") && sign != null && !sign.equals("")) {
				intent = new Intent(activity, OrderListActivity.class);
				startActivity(intent);
			} else {
				Toast.makeText(activity, R.string.check_login_error, Toast.LENGTH_SHORT).show();
			}
			break;
			
		case R.id.member_receiver:
			if (username != null && !username.equals("") && sign != null && !sign.equals("")) {
				intent = new Intent(activity, ReceiverListActivity.class);
				intent.putExtra("referer", "MemberFragment");
				startActivity(intent);
			} else {
				Toast.makeText(activity, R.string.check_login_error, Toast.LENGTH_SHORT).show();
			}
			break;
		
		case R.id.member_favorite:
			if (username != null && !username.equals("") && sign != null && !sign.equals("")) {
				intent = new Intent(activity, FavoriteListActivity.class);
				intent.putExtra("referer", "MemberFragment");
				startActivity(intent);
			} else {
				Toast.makeText(activity, R.string.check_login_error, Toast.LENGTH_SHORT).show();
			}
			break;
			
		case R.id.member_profile:
			if (username != null && !username.equals("") && sign != null && !sign.equals("")) {
				intent = new Intent(activity, ProfileActivity.class);
				intent.putExtra("referer", "MemberFragment");
				startActivity(intent);
			} else {
				Toast.makeText(activity, R.string.check_login_error, Toast.LENGTH_SHORT).show();
			}
			break;
			
		case R.id.member_editpass:
			if (username != null && !username.equals("") && sign != null && !sign.equals("")) {
				intent = new Intent(activity, PasswordActivity.class);
				intent.putExtra("referer", "MemberFragment");
				startActivity(intent);
			} else {
				Toast.makeText(activity, R.string.check_login_error, Toast.LENGTH_SHORT).show();
			}
			break;
			
		case R.id.member_logout:
			SPUtils.clear(activity);
			intent = new Intent(activity, MainActivity.class);
			startActivity(intent);
			activity.finish();
			break;
			
		default:
			break;
		}
	}
}
